package view.main;

import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;

import library.controller.FrameListener;
import library.po.*;

import static java.lang.System.out;

/**
 * @author unknown
 */
public class MainFrame extends JFrame {
    public MainFrame(User user) {
        this.setUser(user);
        initComponents();
    }

    private void initComponents() {
        menuBar = new JMenuBar();
        startMenu = new JMenu();
        reLoginItem = new JMenuItem();
        passwordUpdateItem = new JMenuItem();
        userInfoCheck = new JMenuItem();
        infoUpdateItem = new JMenuItem();
        exitItem = new JMenuItem();
        bookBFMenu = new JMenu();
        borrowMenu = new JMenu();
        viewBookItem = new JMenuItem();
        renewItem = new JMenuItem();
        returnMenu = new JMenu();
        ownBookItem = new JMenuItem();
        bookManageMenu = new JMenu();
        adminViewBookItem = new JMenuItem();
        userManageMenu = new JMenu();
        adminViewUserItem = new JMenuItem();
        picturePanel = new JPanel();
        listener = new FrameListener(this,this.user);

        //======== this ========
        setTitle("\u56fe\u4e66\u7ba1\u7406\u7cfb\u7edf");
        Container contentPane = getContentPane();
        contentPane.setLayout(new GridLayout());

        //======== menuBar ========
        {

            //======== startMenu ========
            {
                startMenu.setText("\u5f00\u59cb\u83dc\u5355");

                //---- reLoginItem ----
                reLoginItem.setText("\u91cd\u65b0\u767b\u9646");
                reLoginItem.addActionListener(listener);
                reLoginItem.setActionCommand("ReLogin");
                startMenu.add(reLoginItem);

                //---- passwordUpdateItem ----
                passwordUpdateItem.setText("\u7528\u6237\u5bc6\u7801\u66f4\u6539");
                passwordUpdateItem.addActionListener(listener);
                passwordUpdateItem.setActionCommand("UpdatePassword");
                startMenu.add(passwordUpdateItem);

                //---- userInfoCheck ----
                userInfoCheck.setText("\u67e5\u770b\u4e2a\u4eba\u4fe1\u606f");
                userInfoCheck.addActionListener(listener);
                userInfoCheck.setActionCommand("CheckPersonalInfo");
                startMenu.add(userInfoCheck);

                //---- infoUpdateItem ----
                infoUpdateItem.setText("\u4fee\u6539\u4e2a\u4eba\u4fe1\u606f");
                infoUpdateItem.addActionListener(listener);
                infoUpdateItem.setActionCommand("UpdatePersonalInfo");
                startMenu.add(infoUpdateItem);
                startMenu.addSeparator();

                //---- exitItem ----
                exitItem.setText("\u6ce8\u9500");
                exitItem.addActionListener(listener);
                exitItem.setActionCommand("Exit");
                startMenu.add(exitItem);
            }
            menuBar.add(startMenu);

            //======== bookBFMenu ========
            {
                bookBFMenu.setText("\u56fe\u4e66\u501f\u8fd8");

                //======== borrowMenu ========
                {
                    borrowMenu.setText("\u501f\u9605");

                    //---- viewBookItem ----
                    viewBookItem.setText("\u6d4f\u89c8\u9986\u5185\u56fe\u4e66");
                    borrowMenu.add(viewBookItem);

                    //---- renewItem ----
                    renewItem.setText("\u7eed\u501f");
                    borrowMenu.add(renewItem);
                }
                bookBFMenu.add(borrowMenu);

                //======== returnMenu ========
                {
                    returnMenu.setText("\u5f52\u8fd8");

                    //---- ownBookItem ----
                    ownBookItem.setText("\u67e5\u770b\u5df2\u501f\u56fe\u4e66");
                    returnMenu.add(ownBookItem);
                }
                bookBFMenu.add(returnMenu);
            }
            menuBar.add(bookBFMenu);

            //======== bookManageMenu ========
            {
                bookManageMenu.setText("\u56fe\u4e66\u7ba1\u7406");

                //---- adminViewBookItem ----
                adminViewBookItem.setText("\u603b\u89c8");
                bookManageMenu.add(adminViewBookItem);
            }
            menuBar.add(bookManageMenu);

            //======== userManageMenu ========
            {
                userManageMenu.setText("\u7528\u6237\u7ba1\u7406");

                //---- adminViewUserItem ----
                adminViewUserItem.setText("\u603b\u89c8");
                userManageMenu.add(adminViewUserItem);
            }
            menuBar.add(userManageMenu);
        }
        setJMenuBar(menuBar);

        //======== picturePanel ========
        {
            picturePanel.setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax. swing. border
            . EmptyBorder( 0, 0, 0, 0) , null, javax. swing. border. TitledBorder. CENTER, javax
            . swing. border. TitledBorder. BOTTOM, new java .awt .Font ("Dia\u006cog" ,java .awt .Font .BOLD ,
            12 ), java. awt. Color. red) ,picturePanel. getBorder( )) ); picturePanel. addPropertyChangeListener (new java. beans
            . PropertyChangeListener( ){ @Override public void propertyChange (java .beans .PropertyChangeEvent e) {if ("bord\u0065r" .equals (e .
            getPropertyName () )) throw new RuntimeException( ); }} );
            picturePanel.setLayout(new GridLayout());
        }
        contentPane.add(picturePanel);
        setSize(825, 505);
        setLocationRelativeTo(getOwner());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void bookAdminLogin(){
        this.bookManageMenu.setEnabled(true);
        this.bookBFMenu.setEnabled(false);
        this.userManageMenu.setEnabled(false);
        this.validate();
    }

    public void sysAdminLogin(){
        this.userManageMenu.setEnabled(true);
        this.bookManageMenu.setEnabled(false);
        this.bookBFMenu.setEnabled(false);
        this.validate();
    }

    public void readerLogin(){
        this.userManageMenu.setEnabled(false);
        this.bookManageMenu.setEnabled(false);
        this.bookBFMenu.setEnabled(true);
    }

    public void setUser(User user){
        this.user = user;
    }
    public FrameListener getMainListener(){
        return this.listener;
    }

    private JMenuBar menuBar;
    private JMenu startMenu;
    private JMenuItem reLoginItem;
    private JMenuItem passwordUpdateItem;
    private JMenuItem userInfoCheck;
    private JMenuItem infoUpdateItem;
    private JMenuItem exitItem;
    private JMenu bookBFMenu;
    private JMenu borrowMenu;
    private JMenuItem viewBookItem;
    private JMenuItem renewItem;
    private JMenu returnMenu;
    private JMenuItem ownBookItem;
    private JMenu bookManageMenu;
    private JMenuItem adminViewBookItem;
    private JMenu userManageMenu;
    private JMenuItem adminViewUserItem;
    private JPanel picturePanel;
    private FrameListener listener;
    private User user;
}
